﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CounterServerModel;
using System.Data;
using CounterServerModel.Authentication;
using Subgurim.Controles;

namespace SmartCounters
{
	public partial class AccountDetails : System.Web.UI.Page
	{
		public static string k_AccountSessionKey = "AccountToUpdate";
		AccountInfo m_AccountToUpdate;
		AccountInfo m_AccountToCreate;

		protected void Page_Load(object sender, EventArgs e)
		{
			((MainTemplate)Master).m_PagePermissionFilter = MainTemplate.k_AdminPageFilter;
			((MainTemplate)Master).ShowHeader = false;
		}

		protected void Page_Prerender(object sender, EventArgs e)
		{
			m_AccountToUpdate = Session[k_AccountSessionKey] as AccountInfo;
			setFromMode();
			tbCountry.Focus();
		}

		private void setFromMode()
		{
			if (m_AccountToUpdate != null)
			{
				btAdd.Visible = false;
				btUpdate.Visible = true;
				loadAccountFormDetails(m_AccountToUpdate);
			}
			else
			{
				btUpdate.Visible = true;
				btUpdate.Visible = false;
			}
		}

		private void loadAccountFormDetails(AccountInfo i_AccountInfo)
		{
			clearForm();
			hdAccountID.Value = i_AccountInfo.AccountID.ToString();
			tbCity.Text = i_AccountInfo.City;
			tbCountry.Text = i_AccountInfo.Country;
			tbStreet.Text = i_AccountInfo.Street;
			tbHouse.Text = i_AccountInfo.House.ToString();
			tbApartment.Text = i_AccountInfo.Apartment.ToString();
			tbLatitude.Text = i_AccountInfo.Latitude.ToString();
			tbLongitude.Text = i_AccountInfo.Longitude.ToString();

		}

		private void getAccoubtInfoFromForm(ref AccountInfo i_AccountInfo)
		{
			String city = this.tbCity.Text;
			String country = this.tbCountry.Text;
			String street = this.tbStreet.Text;
			int house;
			int apartment;
			int.TryParse(this.tbHouse.Text, out house);
			int.TryParse(this.tbApartment.Text, out apartment);
			double latitude;
			double longitude;
			double.TryParse(this.tbLongitude.Text, out longitude);
			double.TryParse(this.tbLatitude.Text, out latitude);

			i_AccountInfo.City = city;
			i_AccountInfo.Country = country;
			i_AccountInfo.Street = street;
			i_AccountInfo.House = house;
			i_AccountInfo.Apartment = apartment;
			i_AccountInfo.Longitude = longitude;
			i_AccountInfo.Latitude = latitude;
		}

		protected void AddAccount(object sender, EventArgs e)
		{
			m_AccountToCreate = new AccountInfo();
			getAccoubtInfoFromForm(ref m_AccountToCreate);

            GMapAccount.Key = "AIzaSyARmX9FYK0psugZwDZWZNPYdWKYJ6U-Hmc";

            string LocationInfo = m_AccountToCreate.Country + " " + m_AccountToCreate.City + " " + m_AccountToCreate.Street + " " + m_AccountToCreate.House;
                
            GeoCode gCode = GMap.geoCodeRequest(LocationInfo, GMapAccount.Key);
            GLatLng nLatLng = new GLatLng(gCode.Placemark.coordinates.lat,gCode.Placemark.coordinates.lng);
            m_AccountToCreate.Latitude = nLatLng.lat;
            m_AccountToCreate.Longitude = nLatLng.lng;
			
            InformationUpdater updateProvider = new InformationUpdater();
			updateProvider.CreateAccount(((MainTemplate)Master).AuthenticatedLoggedInUserCredentials, m_AccountToCreate);
			
			Session[k_AccountSessionKey] = null;
			clearForm();
		}

		protected void UpdateAccount(object sender, EventArgs e)
		{
			m_AccountToUpdate = Session[k_AccountSessionKey] as AccountInfo;
			getAccoubtInfoFromForm(ref m_AccountToUpdate);

			InformationUpdater updateProvider = new InformationUpdater();
			updateProvider.UpdateAccount(((MainTemplate)Master).AuthenticatedLoggedInUserCredentials, m_AccountToUpdate);

			Session[k_AccountSessionKey] = null;
			clearForm();
		}

		protected void Cancel(object sender, EventArgs e)
		{
			Session[k_AccountSessionKey] = null;
			clearForm();
		}

		private void clearForm()
		{
			hdAccountID.Value = "null";
			tbCity.Text = "";
			tbCountry.Text = "";
			tbStreet.Text = "";
			tbHouse.Text = "";
			tbApartment.Text = "";
			tbLatitude.Text = "";
			tbLongitude.Text = "";
		}

		
	}
}